var incrementElementId = 0;

function QElement(type, tagName, namespace) {
    this.elementId = incrementElementId++;
    this.type = type;
    this.tagName = tagName;
    this.parent = undefined;
    this.children = [];
    this.attributes = {};
    var listeners = {};
    this.trigger = function(type, event) {
        if (listeners[type] && listeners[type].length) {
            listeners[type].forEach(function(handler) {
                handler(event);
            });
        }
    }
    // this.$el = qt.createWidget(this,tagName,namespace);

    this.addEventListener = function(name, handler) {
        if (listeners[name] && Array.isArray(listeners[name])) {
            listeners[name].push(handler);
        } else {
            listeners[name] = [];
            listeners[name].push(handler);
        }
        // console.log("addEventListener", name, handler);
    }

    this.setAttribute = function(key, value) {
        this.attributes[key] = value;
        // if(this.$el){
        //   qt.setAttribute(this.$el,key,value);
        // }
        // console.log("setAttribute", key, value);
    }

    this.removeAttribute = function(key) {
        this.attributes[key] = undefined;
        // if(this.$el){
        //   this.$el.removeAttribute(key);
        // }
        // console.log("removeAttribute", key);
    }

    this.insertBefore = function(newNode, referenceNode) {
        let index = this.children.indexOf(referenceNode);
        if (index != -1) {
            this.children.splice(index, 0, newNode);
        }
    }

    this.removeChild = function(child) {
        let index = this.children.indexOf(child);
        if (index !== -1) {
            this.children.splice(index, 1);
            // if(this.$el && child.$el){
            //   this.$el.removeChild(child.$el);
            // }
            // if(child.type === QElement.TEXT){
            //   this.setAttribute("text",undefined);
            // }
        }
    }

    this.nextSibling = function() {
        if (this.parent) {
            let index = this.parent.children.indexOf(this);
            if (index !== -1 && index < this.parent.children.length - 1) {
                return this.parent.children[index + 1];
            }
        }
        return undefined;
    }

    this.appendChild = function(child) {
        child.parent = this;
        this.children.push(child);
        if (child.type === QElement.TEXT) {
            // this.setAttribute("text",child.tagName);
        } else if (child.type === QElement.WIDGET) {
            // if(this.$el && child.$el){
            //   this.$el.appendChild(child.$el);
            // }
        }
    }
}

QElement.TEXT = 0;
QElement.WIDGET = 1;
QElement.SVG = 2;
QElement.COMMENT = 3;
export function createElement(tagName, vnode) {
    // console.log("createElement:" + tagName);
    return new QElement(QElement.WIDGET, tagName);
}

export function createElementNS(namespace, tagName) {
    // console.log("createElementNS", tagName, namespace);
    throw new Error("Did't support Element NS");
    // return new QElement(QElement.SVG,tagName,namespace);
}

export function createTextNode(text) {
    // console.log("createTextNode", text);
    return new QElement(QElement.TEXT, text);
}

export function createComment(text) {
    // console.log("createComment", text);
    let res = new QElement(QElement.COMMENT);
    res.setAttribute("text", text);
    return res;
}

export function insertBefore(parentNode, newNode, referenceNode) {
    // console.log("insertBefore");
    parentNode.insertBefore(newNode, referenceNode);
}

export function removeChild(node, child) {
    // console.log("removeChild");
    node.removeChild(child);
}

export function appendChild(node, child) {
    // console.log("appendChild");
    node.appendChild(child);
}

export function parentNode(node) {
    // console.log("parentNode");
    return node.parent;
}

export function nextSibling(node) {
    // console.log("nextSibling");
    return node.nextSibling();
}

export function tagName(node) {
    // console.log("tagName");
    return node.tagName;
}

export function setTextContent(node, text) {
    // console.log("setTextContent", node.type, text);
    node.tagName = text;
    // if(node.type === QElement.TEXT && node.parent){
    //   node.parent.setAttribute("text",text)
    // }
}

export function setStyleScope(node, scopeId) {
    // console.log("setStyleScope");
    throw new Error("Did't support setStyleScope");
}
